package com.share.service.impl;

import cn.hutool.core.util.IdUtil;
import com.share.constants.Level;
import com.share.constants.UserType;
import com.share.mapper.CustomerSummaryMapper;
import com.share.openfeign.UaaOpenfeign;
import com.share.pojo.domain.Customer;
import com.share.mapper.CustomerMapper;
import com.share.pojo.domain.CustomerSummary;
import com.share.pojo.domain.LoginVo;
import com.share.result.JSONResult;
import com.share.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.utils.BitStatesUtils;
import com.share.utils.NameUtil;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author zcj
 * @since 2025-04-11
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
    @Autowired
    private CustomerSummaryMapper customerSummaryMapper;
    @Autowired
    private UaaOpenfeign uaaOpenfeign;
    @Override
    public JSONResult register(Customer customer) {
        customer.setSex(1);
        customer.setCreateTime(new Date());
        customer.setBitState(BitStatesUtils.OP_PHONE);
        customer.setName(NameUtil.getName());
        customer.setLevel(Level.qingtong.getOrder());
        customer.setId(IdUtil.createSnowflake(0L,0L).nextId());
        super.save(customer);
        //初始化
        CustomerSummary customerSummary = new CustomerSummary();
        customerSummary.setId(customer.getId());
        customerSummary.setCancelOrders(0);
        customerSummary.setTotalFine(new BigDecimal(0));
        customerSummary.setTotalOrders(0);
        customerSummary.setVoucherNumbers(0);
        customerSummaryMapper.insert(customerSummary);
        LoginVo loginVo = new LoginVo();
        loginVo.setType(UserType.user);
        loginVo.setPhone(customer.getPhone());
        loginVo.setOpenId(customer.getOpenId());
        loginVo.setId(customer.getId());
        uaaOpenfeign.register(loginVo);
        return JSONResult.success();
    }
}
